﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.AdvancedSearchCriteria;
using Rio.Applications.Web.Services;
using Rio.Framework.Localisation;

namespace Rio.Framework.Pages.CriteriaControls
{

    public partial class ComparatorControl : ControlBase, INotifyPropertyChanged
    {
        #region Attributs

        private Index _index;

        private string _equalLabel;
        private string _differentLabel;
        private string _betweenLabel;
        private string _superiorLabel;
        private string _superiorEqualLabel;
        private string _inferiorLabel;
        private string _inferiorEqualLabel;

        #endregion

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Called when [notify property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        public void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Property

        /// <summary>
        /// Get or set the current index
        /// </summary>
        public Index Index
        {
            get { return _index; }
            set 
            { 
                _index = value; 
                InitOperatorsList();
                SelectedOperator = Operators.Where(op => op.Value == SqlEqualityOperator.Equal).SingleOrDefault();
            }
        }

        #endregion

        #region Dependency properties

        /// <summary>
        /// Get or set the current Operator
        /// </summary>
        public DisplayOperatorPossible SelectedOperator
        {
            get { return (DisplayOperatorPossible)GetValue(SelectedOperatorProperty); }
            set { SetValue(SelectedOperatorProperty, value); }
        }
        public static readonly DependencyProperty SelectedOperatorProperty =
            DependencyProperty.Register("SelectedOperator", typeof(DisplayOperatorPossible), typeof(ComparatorControl), null);

        /// <summary>
        /// List of available filters
        /// </summary>
        public ObservableCollection<DisplayOperatorPossible> Operators
        {
            get { return (ObservableCollection<DisplayOperatorPossible>)GetValue(OperatorsProperty); }
            set { SetValue(OperatorsProperty, value); }
        }
        public static readonly DependencyProperty OperatorsProperty =
            DependencyProperty.Register("Operators", typeof(ObservableCollection<DisplayOperatorPossible>), typeof(ComparatorControl), null);

        #endregion

        #region Constructors

        public ComparatorControl()
        {
            InitializeComponent();
            InitLabel();
        }

        public ComparatorControl(Index index)
            : this()
        {
            this._index = index;
            InitOperatorsList();
            SelectedOperator = Operators.Where(op => op.Value == SqlEqualityOperator.Equal).SingleOrDefault();
        }

        #endregion

        #region Public functions

        /// <summary>
        /// Init resources
        /// </summary>
        public override void InitLabel()
        {
            _equalLabel = Resx.GetLabel(ResxSearchOperator.EQUAL);
            _differentLabel = Resx.GetLabel(ResxSearchOperator.DIFFERENT);
            _betweenLabel = Resx.GetLabel(ResxSearchOperator.BETWEEN);
            _superiorLabel = Resx.GetLabel(ResxSearchOperator.SUPERIOR);
            _superiorEqualLabel = Resx.GetLabel(ResxSearchOperator.SUPERIOR_EQUAL);
            _inferiorLabel = Resx.GetLabel(ResxSearchOperator.INFERIOR);
            _inferiorEqualLabel = Resx.GetLabel(ResxSearchOperator.INFERIOR_EQUAL);
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Inits the operator list 
        /// </summary>
        private void InitOperatorsList()
        {
            Operators = new ObservableCollection<DisplayOperatorPossible>();

            //Commons operators
            Operators.Add(new DisplayOperatorPossible(_equalLabel, SqlEqualityOperator.Equal));
           
            //Numerical and date only
            switch(this._index.Type)
            {
                case (int)IndexDomainServiceEnums.IndexType.Date :
                case (int)IndexDomainServiceEnums.IndexType.Double:
                case (int)IndexDomainServiceEnums.IndexType.Integer:
                    Operators.Add(new DisplayOperatorPossible(_superiorLabel, SqlEqualityOperator.Superior));
                    Operators.Add(new DisplayOperatorPossible(_superiorEqualLabel, SqlEqualityOperator.SuperiorOrEqual));
                    Operators.Add(new DisplayOperatorPossible(_inferiorLabel, SqlEqualityOperator.Inferior));
                    Operators.Add(new DisplayOperatorPossible(_inferiorEqualLabel, SqlEqualityOperator.InferiorOrEqual));
                    Operators.Add(new DisplayOperatorPossible(_betweenLabel, SqlEqualityOperator.Between));
                    break;
            }

            Operators.Add(new DisplayOperatorPossible(_differentLabel, SqlEqualityOperator.Different));
        }

        #endregion
    }

    /// <summary>
    /// Used to display Operator in combo
    /// </summary>
    public class DisplayOperatorPossible
    {
        public string Name { get; set; }
        public SqlEqualityOperator Value { get; set; }

        public DisplayOperatorPossible()
        {
            Name = String.Empty;
            Value = SqlEqualityOperator.None;
        }

        public DisplayOperatorPossible(string Name, SqlEqualityOperator Value)
        {
            this.Name = Name;
            this.Value = Value;
        }
    }
}
